﻿using System;
using System.IO;
using System.Xml.Serialization;
using MediaPlayer;
using System.Windows.Forms;

namespace SimpleMediaPlayer
{
    public class Settings
    {
        public RenderMode Renderer;
        [XmlElement("DefaultSpeed")]
        public double Speed;
        [XmlElement("DefaultVolume")]
        public int Volume;
        public bool HideIdleCursor;
        public int IdleCursorTimeout;
        public bool HideFullscreenControls;
        public int FullscreenControlsTimeout;
        public bool ShowOSD;
        public bool DisableScreensaver;

        public Settings()
        {
            if (Player.RendererAvalible(RenderMode.EVR))
            {
                this.Renderer = RenderMode.EVR;
            }
            else if (Player.RendererAvalible(RenderMode.VMR9))
            {
                this.Renderer = RenderMode.VMR9;
            }
            else
            {
                this.Renderer = RenderMode.VMR7;
            }

            this.Speed = 1.0;
            this.Volume = 50;
            this.HideIdleCursor = true;
            this.IdleCursorTimeout = 1500;
            this.HideFullscreenControls = true;
            this.FullscreenControlsTimeout = 500;
            this.ShowOSD = true;
            this.DisableScreensaver = true;
        }

        public void Serialize(string file)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Settings));
            StreamWriter writer = File.CreateText(Path.Combine(Application.StartupPath, file));
            serializer.Serialize(writer, this);

            writer.Flush();
            writer.Close();
        }

        public static Settings Deserialize(string file)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Settings));
            StreamReader reader = File.OpenText(Path.Combine(Application.StartupPath, file));
            Settings settings = (Settings)serializer.Deserialize(reader);

            reader.Close();

            return settings;
        }
    }
}
